import axios from 'axios'
const { baseUrl } = require('@/utils/url')

const M_POS = 'post'
const M_PUT = 'put'
const M_DEL = 'delete'
const M_GET = 'get'

const M_POS_PUT = M_POS + M_PUT

//post method= {post | put}
export function httpAction(url, parameter, method, responseType) {
  let obj = {
    url: baseUrl + url,
    method: method,
  }
  if(M_POS_PUT.indexOf(method) > -1){
    obj.data = parameter
  }else{
    obj.params = parameter
  }
  if(responseType){
    obj.responseType = responseType
  }

  return axios(obj).then(res => {
    return res.data
  }).catch(err => {
    return Promise.reject(err)
  })
}

//post
export function postAction(url, parameter) {
  return httpAction(url, parameter, M_POS)
}

//put
export function putAction(url, parameter) {
  return httpAction(url, parameter, M_PUT)
}

//get
export function getAction(url, parameter) {
  return httpAction(url, parameter, M_GET)
}

//delete
export function deleteAction(url, parameter) {
  return httpAction(url, parameter, M_DEL)
}

export function delete2Action(url, parameter) {
  return axios.delete(baseUrl + url,{data:parameter}).then(res => {
    return res.data
  }).catch(err => {
    return err
  })
}

export function downloadByStream(url, parameter, method=M_POS) {
  return httpAction(url, parameter, method, 'blob')
}

export function delete3Action(url, parameter) {
  return axios.delete(baseUrl + url + '/'+parameter).then(res => {
    return res.data
  }).catch(err => {
    return err
  })
}